/*
 * C4.4.2.c
 *
 *  Created on: 2012/10/25
 *      Author: hengzhong
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "rule_info.h"
#include "core.h"
#include "trace.h"

static const PSZ RuleID = "C4.4.2";

static const PSZ MESSAGE_ENG = "Unsigned expression negated";
static const PSZ MESSAGE_JPN = "単項演算子'-'は符号無しの式に使用";
static const PSZ MESSAGE_CHS = "对无符号型的表达式使用了一元减运算符";

static const U2 RANK = MSG_RANK_ERR;

static PRULEOUT pRuleHead;
static PRULEOUT pRuleTail;

/******************************************************************
 * static function:
 * RuleCheck
 *******************************************************************/
static INT RuleCheck(AST cmdIndex) {
	AST astOperand = 0;
	AST typeIndex = 0;
	INT result = RULE_RESULT_OK;
	if (LinkCommandList.isSingleMinus(cmdIndex)) {
		astOperand = LinkCommandList.getLeftOperand(cmdIndex);
		typeIndex = LinkCommandList.getType(astOperand);
		if (LinkTypeInfo.isUnSigned(typeIndex)) {
			result = RULE_RESULT_NG;
		}
	}

	return result;
}

/******************************************************************
 * main logic
 *******************************************************************/
static S4 RuleMain(AST cmdIndex, U4 event) {

	INT result = RULE_RESULT_OK;
	PRULEOUT newNode = NULL;
	PRULEOUT tmpNode = NULL;
	PRULEOUT tmpNode1 = NULL;

	AST codeScp = 0;

	if (!(event & EVENT_CMDLIST_PREV)) {
		return EXIT_SUCCESS;
	}

	result = RuleCheck(cmdIndex);

	/*re-checked?*/
	tmpNode = pRuleHead;
	while (tmpNode != NULL ) {
		if (tmpNode->indexInfo == cmdIndex) {
			if (result > RULE_RESULT_OK) {
				result = RULE_RESULT_OK;
			} else {
				if (tmpNode == pRuleHead) {
					pRuleHead = pRuleHead->next;
					free(tmpNode);
				} else if (tmpNode == pRuleTail) {
					tmpNode1->next = NULL;
					pRuleTail = tmpNode1;
					free(tmpNode);
				} else {
					tmpNode1->next = tmpNode->next;
					free(tmpNode);
				}
			}
			break;
		}
		tmpNode1 = tmpNode;
		tmpNode = tmpNode->next;
	}

	/*make result struct*/
	if (result > RULE_RESULT_OK) {
		newNode = (PRULEOUT) malloc(sizeof(RULEOUT));
		if (pRuleHead == NULL ) {
			pRuleHead = newNode;
		}

		codeScp = LinkCommandList.getCodeScope(cmdIndex);
		newNode->indexInfo = cmdIndex;
		newNode->fileIndex = CCodeScope.getFileIndex(codeScp);
		newNode->beginLine = CCodeScope.getBeginLine(codeScp);
		newNode->beginCol = CCodeScope.getBeginLineOffset(codeScp);
		newNode->endLine = CCodeScope.getEndLine(codeScp);
		newNode->endCol = CCodeScope.getEndLineOffset(codeScp);
		newNode->ruleID = RuleID;
		newNode->errRank = RANK;

		sprintf(newNode->msgBuf_eng, MESSAGE_ENG);
		sprintf(newNode->msgBuf_chs, MESSAGE_CHS);
		sprintf(newNode->msgBuf_jpn, MESSAGE_JPN);

		newNode->rfileIndex = 0;
		newNode->rlocLine = 0;
		newNode->next = NULL;

		if (pRuleTail != NULL ) {
			pRuleTail->next = newNode;
		}

		pRuleTail = newNode;
	}

	return EXIT_SUCCESS;
}

VOID C4_4_2_register(VOID) {
	PRULEMAN * pCurRule = &stRuleManager;
	PRULEMAN newRuleMan = (PRULEMAN) malloc(sizeof(RULEMAN));
	memset(newRuleMan, 0, sizeof(RULEMAN));
	newRuleMan->ruleID = RuleID;
	newRuleMan->ruleSwitch = OFF;
	newRuleMan->headOut = &pRuleHead;
	newRuleMan->tailOut = &pRuleTail;
	newRuleMan->ruleMain_cmd = RuleMain;
	newRuleMan->ruleMain_ast = NULL;
	newRuleMan->nextRule = NULL;
	if (stRuleManager == NULL ) {
		stRuleManager = newRuleMan;
	} else {
		while ((*pCurRule)->nextRule != NULL ) {
			pCurRule = &((*pCurRule)->nextRule);
		}
		(*pCurRule)->nextRule = newRuleMan;
	}
}

